﻿//test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using NL.ADA.AdminService;
using System.ServiceModel;
using NL.ADA.Common;
using NL.ADA.ConfigService.DAL.TypedListClasses;
using NL.ADA.ConfigService.DAL.HelperClasses;
using NL.ADA.ConfigService.DAL.EntityClasses;

namespace NL.ADA.UI
{
    public class Gateway : IDisposable
    {
        public string ApplicationCode { get; set; }
        public string Mode { get; set; }
        public int ModeId { get; set; }
        public int ApplicationId { get; set; }
        private IAdminService _service;
        private ChannelFactory<IAdminService> _channelFactory;

        public Gateway()
        {
            _channelFactory = new ChannelFactory<IAdminService>("WSHttpBinding_IAdminService");
            ApplicationCode = ConfigurationManager.AppSettings["applicationcode"];
            Mode = ConfigurationManager.AppSettings["mode"];
            _service = _channelFactory.CreateChannel();

        }

        public EntityCollection GetModes()
        {
            return _service.GetAllModes();
        }
        public EntityCollection GetConfigTypes()
        {
            return _service.GetAllConfigTypes();
        }

        public int InsertConfig(int configId, int modeId, string key, string value, int applicationId, string applicationCode, string configTypeName, int configTypeId)
        {
            var config = new CustomConfig();
            config.ConfigId = configId;
            config.ModeId = modeId;
            config.Key = key;
            config.Value = value;
            config.ApplicationId = applicationId;
            config.ConfigTypeName = configTypeName;
            config.ConfigTypeId = configTypeId;
            return _service.InsertConfig(config);

        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, false)]
        public int UpdateConfig(int configId, int modeId, string key, string value, int applicationId, string configTypeName, int configTypeId)
        {
            var config = new CustomConfig();
            config.ConfigId = configId;
            config.ModeId = modeId;
            config.Key = key;
            config.Value = value;
            config.ApplicationId = applicationId;
            config.ConfigTypeName = configTypeName;
            config.ConfigTypeId = configTypeId;
            return _service.UpdateConfig(config);

        }
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Fill, false)]
        public EntityCollection GetAllApplications()
        {
            return _service.GetApplications();
        }

        public CustomConfigList GetAllConfigs(int appId, int modeId)
        {
            return _service.GetAllConfigs(appId, modeId);
        }
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
        public ApplicationEntity SelectApplicationById(int id)
        {
            var result  = _service.SelectApplicationById(id);
            return result;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, false)]
        public int InsertApplication(string appCode, string description)
        {
            var applicationEntity = new ApplicationEntity
                                        {
                                            AppCode = appCode,
                                            Description = description,
                                            Created = DateTime.Now,
                                            Modified = DateTime.Now
                                        };
            return _service.InsertApplication(applicationEntity);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, false)]
        public int UpdateApplication(int id, string appCode, string description)
        {
            var applicationEntity = new ApplicationEntity
                                        {
                                            Id = id,
                                            AppCode = appCode,
                                            Description = description,
                                            Created = DateTime.Now,
                                            Modified = DateTime.Now
                                        };
            return _service.InsertApplication(applicationEntity);
        }

        public void Dispose()
        {
            if (_channelFactory != null)
                _channelFactory.Close();
        }


        public bool Delete(int configId)
        {
            return _service.Deleteconfig(configId);
        }
    }
}
